package com.daocao.ds.constant;

import android.text.TextUtils;

import com.daocao.ds.Application;
import com.daocao.ds.api.model.InitAppModel;
import com.daocao.ds.api.model.UserModel;
import com.daocao.ds.event.LoginSuccessEvent;
import com.daocao.ds.event.LogoutEvent;
import com.daocao.ds.util.DateTime;
import com.daocao.ds.util.PreferenceUtils;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by xsooy-pc on 18-3-7.
 */

public class UserInfoGlobal {
    public static String date;

    public static void saveInitData(InitAppModel.DatasBean item) {
        PreferenceUtils.putString(Application.getInstance(), "is_login", item.is_logined);
        PreferenceUtils.putString(Application.getInstance(), "lastest_version", item.version.lastest_version);
        PreferenceUtils.putString(Application.getInstance(), "download_url", item.version.download_url);
        PreferenceUtils.putString(Application.getInstance(), "upgrade_msg", item.version.upgrade_msg);
        PreferenceUtils.putString(Application.getInstance(), "service_phone", item.contact_info.service_phone);
        PreferenceUtils.putInt(Application.getInstance(), "forced_upgrade", item.version.forced_upgrade);


        PreferenceUtils.putString(Application.getInstance(), "bankcard_no", item.gathering.bankcard_no);
        PreferenceUtils.putString(Application.getInstance(), "name", item.gathering.name);
        PreferenceUtils.putString(Application.getInstance(), "bankzone", item.gathering.bankzone);

        PreferenceUtils.putString(Application.getInstance(), "day_limit", item.bank_limit.day_limit);
        PreferenceUtils.putString(Application.getInstance(), "month_limit", item.bank_limit.month_limit);

    }

    public static void saveUserData(UserModel.DatasBean item) {
        PreferenceUtils.putString(Application.getInstance(), "balances", item.balances);
        PreferenceUtils.putString(Application.getInstance(), "total", item.total);
        PreferenceUtils.putString(Application.getInstance(), "phone", item.loaner.mobile);
        PreferenceUtils.putString(Application.getInstance(), "auto_loan_rule", item.loaner.auto_loan_rule);
    }

    public static boolean getAutoRule() {
        if (PreferenceUtils.getString(Application.getInstance(), "auto_loan_rule", "OFF").equals("OFF")) {
            return false;
        } else {
            return true;
        }
    }

    public static void saveAutoRule(String auto_loan_rule) {
        PreferenceUtils.putString(Application.getInstance(), "auto_loan_rule", auto_loan_rule);
    }


    public static String getBalances() {
        return PreferenceUtils.getString(Application.getInstance(), "balances", "");
    }

    public static String getTotal() {
        return PreferenceUtils.getString(Application.getInstance(), "total", "");
    }

    public static String getUserPhone() {
        return PreferenceUtils.getString(Application.getInstance(), "phone", "");
    }


    public static String getUserNick() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "username", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "username", "");
        } else {
            return PreferenceUtils.getString(Application.getInstance(), "phone", "");
        }
    }

    public static String getUserIcon() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "icon", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "icon", "");
        } else {
            return "";
        }
    }


    public static void saveNickName(String name) {
        PreferenceUtils.putString(Application.getInstance(), "nickname", name);
    }

    public static void saveUserSex(String sex) {
        PreferenceUtils.putString(Application.getInstance(), "sex", sex);
    }

    public static String getHeader() {
        return PreferenceUtils.getString(Application.getInstance(), "header", "");
    }

    public static void login() {
        PreferenceUtils.putString(Application.getInstance(), "is_login", "1");
        EventBus.getDefault().post(new LoginSuccessEvent());
        saveGoLogoutTime();
    }

    public static void logout() {
        PreferenceUtils.putString(Application.getInstance(), "token", "");
        PreferenceUtils.putString(Application.getInstance(), "is_login", "");
        EventBus.getDefault().post(new LogoutEvent());
    }

    public static void setIsOpenGuesture(boolean isOpenGuesture) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture", isOpenGuesture);
    }

    public static void setIsOpenGuestureRoot(boolean isGestrueRoot) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture_root", isGestrueRoot);
    }

    public static boolean getIsOpenGuestureRoot() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture_root", true);
    }

    public static boolean getIsOpenGuesture() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture", false);
    }

    public static boolean checkLogin() {

        if (PreferenceUtils.getString(Application.getInstance(), "is_login", "0").equals("1")) {

            return true;
        }
        return false;
    }

    public static void saveGoLogoutTime() {
        PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
    }

    public static boolean isNeedLogout() {
        if (System.currentTimeMillis() - PreferenceUtils.getLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis()) > wait_time) {
            return true;
        } else {
            PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
            return false;
        }
    }


    public static long wait_time = 1000 * 60 * 120;


    public static boolean getIsSetPayPass() {
        if (PreferenceUtils.getString(Application.getInstance(), "isset_pay_password", "0").equals("1")) {
            return true;
        } else {
            return false;
        }
    }


    public static void setPayPass() {
        PreferenceUtils.putString(Application.getInstance(), "isset_pay_password", "1");
    }
}
